using System;

namespace SubForum.Framework.Utilities 
{
    public static class ShortGuid
    {
        /// <summary>
        /// Encodes the specified GUID text.
        /// </summary>
        /// <param name="guidText">The GUID text.</param>
        /// <returns></returns>
        public static string Encode(string guidText)
        {
            var guid = new Guid(guidText);

            return Encode(guid);
        }

        /// <summary>
        /// Encodes the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public static string Encode(Guid guid)
        {
            var enc = Convert.ToBase64String(guid.ToByteArray());
            enc = enc.Replace("/", "_");
            enc = enc.Replace("+", "-");

            return enc.Substring(0, 22);
        }

        /// <summary>
        /// Decodes the specified encoded.
        /// </summary>
        /// <param name="encoded">The encoded.</param>
        /// <returns></returns>
        public static Guid Decode(string encoded)
        {
            encoded = encoded.Replace("_", "/");
            encoded = encoded.Replace("-", "+");
            var buffer = Convert.FromBase64String(encoded + "==");

            return new Guid(buffer);
        }
    }
}